import 'package:bigegg/entity/articel/xmm_article_info.dart';
import 'package:bigegg/global/user/xmm_account_manager.dart';
import 'package:get/get.dart';

import '../../../../common/get/controller/xmm_base_controller.dart';
import '../../../../global/xmm_app_const.dart';
import '../../../../utils/xmm_event_bus.dart';

class XmmHomeController extends XmmBaseGetxController {
  var topArticleList = <XmmArticleInfo>[].obs;

  var hotArticleList = <XmmArticleInfo>[].obs;

  @override
  void onReady() {
    super.onReady();
    XmmEventBus.instance.on(XmmAppConst.eventBlockStatusUpdateKey, (arg) => initData());
  }

  @override
  void onClose() {
    XmmEventBus.instance.off(XmmAppConst.eventBlockStatusUpdateKey);
    super.onClose();
  }

  void initData() {
    ///顶部推荐列表数据
    var userList = XmmAccountManager.instance.wallUserList
        .where((element) => element.isBlack != true); //不展示被拉黑的。
    if (userList.length > 3) {
      userList = userList.take(3);
    }
    var topList = <XmmArticleInfo>[];
    for (var element in userList) {
      var articleList = XmmAccountManager.instance.findArticle(element.author ?? "");
      topList.add(articleList.first);
    }
    topArticleList.value = topList;
    topArticleList.refresh();

    ///列表数据
    var articleList = XmmAccountManager.instance.articleInfoList
        .where((element) => element.wallUser?.isBlack != true); //不展示被拉黑的。
    hotArticleList.value = articleList.toList();
    hotArticleList.refresh();
  }
}
